package com.jdzy.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdzy.model.common.vos.ResultA;
import com.jdzy.model.common.vos.ResultCodeEnum;
import com.jdzy.model.student.dtos.AppointmentsDto;
import com.jdzy.model.student.dtos.EventAnnouncementsDto;
import com.jdzy.model.student.dtos.JobPostingsDto;
import com.jdzy.model.student.pojos.EventAnnouncements;
import com.jdzy.model.student.pojos.JobPostings;
import com.jdzy.model.student.vos.EventAnnouncementsVo;
import com.jdzy.model.student.vos.JobPostingsVo;
import com.jdzy.model.teacher.vos.SysMenuVo;
import com.jdzy.student.service.AppointmentsService;
import com.jdzy.student.service.EventAnnouncementsService;
import com.jdzy.student.service.JobPostingsService;
import com.jdzy.student.service.SysMenuService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(allowCredentials = "true" , allowedHeaders = "*")
@Slf4j
@RequestMapping("api/index")
@Api(value = "首页的控制类")
@RequiredArgsConstructor
public class IndexController {
private final EventAnnouncementsService eventAnnouncementsService;
private final JobPostingsService jobPostingsService;
private final AppointmentsService appointmentsService;

private  final SysMenuService sysMenuService ;

    /**
     * 分页获取岗位推荐信息
     */

    @GetMapping("getJobInfo")
    public ResultA<IPage<JobPostingsVo>> getJobInfo(@RequestParam long current, @RequestParam long size) {
        IPage<JobPostings> jobPostingsPage = jobPostingsService.getJobPostingsPage(current, size);
        List<JobPostingsVo> jobPostingsVoList = new ArrayList<>();
        for (JobPostings jobPosting : jobPostingsPage.getRecords()) {
            JobPostingsVo jobPostingsVo = new JobPostingsVo();
            BeanUtils.copyProperties(jobPosting, jobPostingsVo);
            jobPostingsVoList.add(jobPostingsVo);
        }
        IPage<JobPostingsVo> jobPostingsVoPage = new Page<>(current, size, jobPostingsPage.getTotal());
        jobPostingsVoPage.setRecords(jobPostingsVoList);

        return ResultA.ok(jobPostingsVoPage);
    }

    /**
     * 保存或修改岗位
     */

    @PostMapping("saveOrUpdateJob")
    public ResultA<String> saveOrUpdateJob(@RequestBody JobPostingsDto jobPostingsDto) {
         jobPostingsService.saveOrUpdateJob(jobPostingsDto);
        return ResultA.ok("保存或修改成功");
    }


    /**
       * 删除岗位信息
      */
    @DeleteMapping("deleteJob")
    public ResultA<String> deleteJob(@RequestParam Long id) {
        jobPostingsService.removeById(id);
        return ResultA.ok("删除成功");
    }


    /**
     * 分页获取赛事预告信息
     */
    @GetMapping("getEventInfo")
    public ResultA<IPage<EventAnnouncementsVo>> getEventInfo(@RequestParam long current, @RequestParam long size) {
        IPage<EventAnnouncements> eventAnnouncementsPage = eventAnnouncementsService.getEventAnnouncementsPage(current, size);
        List<EventAnnouncementsVo> eventAnnouncementsVoList = new ArrayList<>();
        for (EventAnnouncements eventAnnouncements : eventAnnouncementsPage.getRecords()) {
            EventAnnouncementsVo eventAnnouncementsVo = new EventAnnouncementsVo();
            BeanUtils.copyProperties(eventAnnouncements, eventAnnouncementsVo);
            eventAnnouncementsVoList.add(eventAnnouncementsVo);
        }
        IPage<EventAnnouncementsVo> eventAnnouncementsVoPage = new Page<>(current, size, eventAnnouncementsPage.getTotal());
        eventAnnouncementsVoPage.setRecords(eventAnnouncementsVoList);

        return ResultA.ok(eventAnnouncementsVoPage);
    }


    /**
     * 保存或修改赛事预告
     */
    @PostMapping("saveOrUpdateEvent")
    public ResultA<String> saveOrUpdateEvent(@RequestBody EventAnnouncementsDto eventAnnouncementsDto) {
        eventAnnouncementsService.saveOrUpdateEvent(eventAnnouncementsDto);
        return ResultA.ok("保存或修改成功");
    }

    /**
     * 删除赛事预告信息
     * @param id
     * @return
     */
    @DeleteMapping("deleteEvent")
    public ResultA<String> deleteEvent(@RequestParam Long id) {
        eventAnnouncementsService.removeById(id);
        return ResultA.ok("删除成功");
    }

    /**
     * 一对一教师预约功能
     */
    @PostMapping("/makeAppointment")
    public ResultA<String> makeAppointment(@RequestBody AppointmentsDto appointmentsDto) {
        return appointmentsService.makeAppointment(appointmentsDto);
    }

    /**
     * 此处是教师端的首页
     * @return
     */
    //在IndexController中添加获取菜单接口方法：-->动态菜单
    @GetMapping("/menus/{id}")
    public ResultA menus(@PathVariable Long id) {
        List<SysMenuVo> sysMenuVoList =  sysMenuService.findUserMenuList(id) ;
        return ResultA.build(sysMenuVoList , ResultCodeEnum.SUCCESS) ;
    }

}


